<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实现跨域登录供能 </title>
</head>

<body>
    <form id="form">
        <input type="text" name="username" value='itheima' /><br>
        <input type="password" name="password" value='123456' /><br>
        <input type="button" value="登录" id='login' />
        <input type="button" value="检测登录状态" id='check' />
    </form>
    <script>
        var form = document.getElementById('form');
        var login = document.getElementById('login');
        var check = document.getElementById('check');
        login.onclick = function() {
            var formData = new FormData(form);
            var xhr = new XMLHttpRequest();
            xhr.open('post', '/login');
            // 当发送跨域请求时，携带cookie信息
            xhr.withCredentials = true;
            xhr.send(formData);
            xhr.onload = function() {
                if (xhr.status == 200) {
                    console.log(xhr.responseText);
                }
            }
        }
        check.onclick = function() {
            var xhr = new XMLHttpRequest();
            xhr.open('get', '/check');
            // 当发送跨域请求时，携带cookie信息
            xhr.withCredentials = true;
            xhr.send();
            xhr.onload = function() {
                if (xhr.status == 200) {
                    console.log(xhr.responseText);
                }
            }
        }
    </script>
</body>

</html>